package com.qunchuang.netty;

import com.qunchuang.mapper.LiftAgvcTaskMapper;
import com.qunchuang.mapper.LiftMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
 class ServerHandlerInit extends ChannelInitializer<SocketChannel> {
     private final LiftAgvcTaskMapper liftAgvcTaskMapper;
     //    @Autowired
     private final LiftMapper liftMapper;

     private ReconnectClient reconnectClient;
     @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        // 解码出具体的数据类型
        //该篇对解码器和编码器进行了详细说明，推荐阅读https://blog.csdn.net/tang_huan_11/article/details/133853786
//            pipeline.addLast("decoder", new StringDecoder());//解码和编码可以自己定义
//            pipeline.addLast(new Handler());//handler类是自己写的处理类
//            pipeline.addLast("encoder", new StringEncoder());
//        ByteBuf delimiter1 = Unpooled.copiedBuffer("aa".getBytes());
//        ByteBuf delimiter2 = Unpooled.copiedBuffer("bb".getBytes());
//        pipeline.addLast(new DelimiterBasedFrameDecoder(1024, delimiter1, delimiter2));
//        pipeline.addLast("decoder", new StringDecoder());//解码和编码可以自己定义
//        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,1,1,-1,2));
        pipeline.addLast(new Handler(liftAgvcTaskMapper,liftMapper,reconnectClient));
    }
}
